package com.woniuxy.main.dynamic_lzq.hander;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.dynamic_lzq.dto.UserVenueDto;
import com.woniuxy.main.dynamic_lzq.pojo.User;
import com.woniuxy.main.dynamic_lzq.pojo.Venue_lzq;
import com.woniuxy.main.dynamic_lzq.service.UserService_Lzq;
import com.woniuxy.main.dynamic_lzq.service.VenueService_Lzq;
import com.woniuxy.main.dynamic_lzq.test.MapDistance;
import com.woniuxy.main.login_yjj.pojo.User_Yjj;
import com.woniuxy.main.login_yjj.service.LoginService;

import lombok.Data;

@Data
@Controller
public class VenueHandler_Lzq {
	@Autowired
	private UserService_Lzq userService;
	@Autowired
	private LoginService loginService;
	@Autowired
	private VenueService_Lzq venueService;
	
	@RequestMapping("/showvenueIndistance")
	@ResponseBody
	public UserVenueDto showVenueIndistance(HttpServletRequest request){
		User_Yjj currentUser =  (User_Yjj) request.getSession().getAttribute("currentUser");
		//用户所在经度、纬度
		double  userLongitude=currentUser.getLongitude();	
		double userLatitude=currentUser.getLatitude();
		
		List<User> users = userService.listVenues();
		
		venueDistance(userLongitude,userLatitude,users);
		users = sortDistance();
		
		List<Venue_lzq> venues = venueService.findVenuesByUsers(users);
		
		UserVenueDto dto = new UserVenueDto(users,venues);
		 return dto;
	}
	
	
	@RequestMapping("/showstudentIndistance")
	@ResponseBody
	public List<User> showStudentIndistance(HttpServletRequest request){
		User_Yjj currentUser =  (User_Yjj) request.getSession().getAttribute("currentUser");
		//用户所在经度、纬度
		double  userLongitude=currentUser.getLongitude();	
		double userLatitude=currentUser.getLatitude();
		
		List<User> users = userService.listStudents();
		
		venueDistance(userLongitude,userLatitude,users);
		users = sortDistance();
		
		
		 return users;
	}
	
	
	
	
	
	static TreeMap<Double, User> distanceMap = new TreeMap<Double, User>();
	//遍历场馆距学员的距离
	public static void venueDistance(double userLongitude,double userLatitude,List<User> users){
		for(int i=0;i<users.size();i++){
			User user = users.get(i);
			double venueLongitude=user.getLongitude();
			double venueLatityde = user.getLatitude();
			//source的经纬度，target的经纬度
			double distance =  MapDistance.findDistance(userLongitude,userLatitude,venueLongitude,venueLatityde);
			user.setDistance(distance);
			
			distanceMap.put(distance, user);
		}
	}
	
	//按场馆距离进行排序
	private static List<User> sortDistance() {
		List<User> users = new ArrayList<>();
	    Iterator<Entry<Double, User>> it = distanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Double, User> entry = it.next();
          /*  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());*/
            users.add(entry.getValue());
        }
        distanceMap.clear();
		return users;
		
	}
	
	
	
	@RequestMapping("/showVenueInfo")
	public ModelAndView showInfo(int user_id){
		ModelAndView modelAndView  = new ModelAndView();
		User user  = userService.findUserByUid(user_id);
		modelAndView.addObject("user", user);
		modelAndView.setViewName("venuedetail.html");
		return modelAndView;
	}
}
